﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using spbtzb;

public partial class SearchResult : System.Web.UI.Page
{
    /// <summary>
    /// 对应信息的父类型
    /// </summary>
    private String StrConn = ConString.ConnString;
    private String strType = "";
    private String strKeys ="";
    private String view_Type
    {
        get
        {
            return ViewState["view_Type"] == null ? "" : Convert.ToString(ViewState["view_Type"]);
        }
        set
        {
            ViewState["view_Type"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
   
        if (Request.Params["KeyWords"] != null && Request.Params["Type"] != null)
        {
            strKeys = Request.Params["KeyWords"].ToString();
            strType = Request.Params["Type"].ToString();
        }
        if (!Page.IsPostBack)
        {
            this.Page.Title = "查询结果页面";
            dvbtn.Visible = false;
            Ddl_Types_BindData();
            if (Request.Params["KeyWords"] != null && Request.Params["Type"] != null)
            {
                ltResult.Text = "<font color =red>0</font>";
                view_Type = strType;
                if (String.IsNullOrEmpty(strType))
                {
                    Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "页面参数为空，请联系管理员!");
                }
                else
                {
                    foreach (ListItem item in ddl_Types.Items)
                    {
                        if (item.Value == strType)
                        {
                            item.Selected = true;
                        }
                    }
                }
                this.lbCurPage.Text = "1";
                BindControl();
            }
        }
    }
    protected void BindControl()
    {
        String strNakeKeys = strKeys.Trim();
        // analysis the strKeys       
        if (String.IsNullOrEmpty(strNakeKeys) || String.Equals(strKeys, "请输入关键字"))
        {
            Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "非法操作，关键字为空!");

        }
        strKeys = strKeys.Trim() + " ";//注意后面加了一个空格
        ArrayList alKeys = new ArrayList();
        Int32 iIndexEmp = 0;
        while (1 == 1)
        {
            iIndexEmp = strKeys.IndexOf(' ');
            if (iIndexEmp < 0) break;
            String strTemp = strKeys.Substring(0, iIndexEmp);
            alKeys.Add(strTemp);
            strKeys = strKeys.Substring(iIndexEmp);
            strKeys = strKeys.TrimStart();
        }
        ViewState["alKeys"] = alKeys;
        String strCmd = "";
        ltKeyWords.Text = "";
        TB_Search.Text = "";
        if (strType == "all")
        {
            strCmd = "select a.id,a.title,a.content,a.createtime,a.viewtimes,b.typename " +
                     "from contents a, contenttype b WHERE a.typecode = b.typecode";
            foreach (String s in alKeys)
            {
                strCmd += String.Format(" AND a.Title like '%{0}%' ", s);
                ltKeyWords.Text += "&nbsp;<font color =red>" + s + "</font>";
                TB_Search.Text += s + " ";
            }
        }
        else // 具体类型
        {
            strCmd = String.Format("select a.id,a.title, a.content, a.createtime, a.viewtimes,b.typename from contents a, contenttype b"
                                   + " where a.typecode = b.typecode and b.parenttype =  '{0}'", strType);
            
            foreach (String s in alKeys)
            {
                strCmd += String.Format(" AND a.Title like '%{0}%'  ", s);
                ltKeyWords.Text += "<font color =red>" + s + "</font>&nbsp;";
                TB_Search.Text += s + " ";
            }
        }
        strCmd += " ORDER BY a.createtime desc, a.id";
        DataTable dt = AccessHelper.ExecuteDataTable(StrConn, CommandType.Text, strCmd);
        ltResult.Text = "<font color =red>" + dt.Rows.Count + "</font>";
        //rplist.DataSource = pds();
        //rplist.DataBind();
        if (dt.Rows.Count > 0) dvbtn.Visible = true;
        PagedDataSource ps = new PagedDataSource();
        ps.AllowPaging = true;
        ps.PageSize = 5;
        rplist.DataSource = ps;
        ps.CurrentPageIndex = Convert.ToInt32(this.lbCurPage.Text) - 1;
        ps.DataSource = dt.DefaultView;
        this.btnPreviousPage.Enabled = true;
        this.btnNextPage.Enabled = true;
        if (ps.CurrentPageIndex == 0)
        {
            this.btnPreviousPage.Enabled = false;
        }
        if (ps.CurrentPageIndex == ps.PageCount - 1)
        {
            this.btnNextPage.Enabled = false;
        }
        rplist.DataBind();


    }
    protected string TrimHtml(string html)
    {
        html = Common.regularExpressionsOfHTML(html);
        if (html.Length > 200)
        {
            html = Common.CutString(html, 200);
        }
        return html;
    }

    protected void btn_Search_Click(object sender, EventArgs e)
    {

        Response.Redirect("SearchResult.aspx?Type=" + ddl_Types.SelectedValue + "&KeyWords=" + TB_Search.Text);

    }

    private void Ddl_Types_BindData()
    {
        // generate datatable                       
        string sqlCmd = "SELECT * FROM ContentType WHERE ParentType = 'NO'";
        System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
        if (dtTypes == null || dtTypes.Rows.Count == 0)
        {
            // 没有相应类型的信息
            // RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            return;
        }
        this.ddl_Types.DataSource = dtTypes;
        this.ddl_Types.DataBind();
        this.ddl_Types.Items.Add(new ListItem("全部", "all"));
    }

    protected void btnNextPage_Click(object sender, System.EventArgs e)
    {
        this.lbCurPage.Text = Convert.ToString(Convert.ToInt32(this.lbCurPage.Text) + 1);
        this.BindControl();
    }

    protected void btnPreviousPage_Click(object sender, System.EventArgs e)
    {
        this.lbCurPage.Text = Convert.ToString(Convert.ToInt32(this.lbCurPage.Text) - 1);
        this.BindControl();
    }
    protected string BoldString(string str)
    {
        if (ViewState["alKeys"] != null)
        {
            ArrayList alKeys = (ArrayList)ViewState["alKeys"];
            foreach (String s in alKeys)
            {
                str = str.Replace(s, "<b>" + s + "</b>");
            }
        }
        return str;
    }


}
